package com.sdkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sdkj.mapper.OrdersettingMapper;
import com.sdkj.pojo.Ordersetting;
import com.sdkj.service.OrdersettingService;
import com.sdkj.util.POIUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrdersettingServiceImpl implements OrdersettingService {
    @Resource
    private OrdersettingMapper ordersettingMapper;
;

    //上传文件 持久化预约信息
    @Override
    public boolean saveOrdersettingInfo(List<String[]> dataInfo) {
        //[[“2024/10/22”,"300"],[“2024/10/23”,"300"],[“2024/10/24”,"300"]..]
        boolean flag = false;
        if (dataInfo!=null && dataInfo.size()>0){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            for (String[] strings : dataInfo) {
                //[“2024/10/22”,"300"]
                String dateStr = strings[0];
                int number = Integer.parseInt(strings[1]);
                try {
                    //将一个字符串数据变成日期类型的数据
                    Date date = simpleDateFormat.parse(dateStr);
                    //判断该日期是否已经提前设定了预约信息
                    LambdaQueryWrapper<Ordersetting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(Ordersetting::getOrderdate,date);
                    Ordersetting ordersetting = ordersettingMapper.selectOne(lambdaQueryWrapper);
                    if (ordersetting == null){
                        //添加动作
                        ordersetting = new Ordersetting();
                        ordersetting.setOrderdate(date);
                        ordersetting.setNumber(number);
                        flag = ordersettingMapper.insert(ordersetting)>0;
                    }else{
                        //更新动作
                        if (number < ordersetting.getReservations()){
                            ordersetting.setNumber(ordersetting.getReservations());
                        }else{
                            ordersetting.setNumber(number);
                        }
                        flag = ordersettingMapper.updateById(ordersetting)>0;
                    }
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        }
        return flag;
    }
    @Override
    public List<Map<String, Object>> getOrdersettingInfo(String date) {
        List<Map<String, Object>> list = new ArrayList<>();
        // "2024-10"
        String  startTime = date+"-01"; // "2024-10-01"
        String  endTime = date+"-31"; //"2024-10-31"
        //按照条件查询数据
        LambdaQueryWrapper<Ordersetting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(Ordersetting::getOrderdate,startTime,endTime);
        List<Ordersetting> ordersettings = ordersettingMapper.selectList(lambdaQueryWrapper);
        if (ordersettings!=null && ordersettings.size()>0){
            for (Ordersetting ordersetting : ordersettings) {
                Map<String,Object> map = new HashMap<>();
                int date1 = ordersetting.getOrderdate().getDate();
                map.put("date",date1);
                map.put("number",ordersetting.getNumber());
                map.put("reservations",ordersetting.getReservations());
                list.add(map);
            }
        }
        return list;
    }
    //单独设定某一天的预约人数
    @Override
    public boolean updateOrdersettingInfo(String date, Integer number) {
        boolean flag = false;
        //更新预约信息
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try{
            Date newDate = simpleDateFormat.parse(date);
            LambdaQueryWrapper<Ordersetting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Ordersetting::getOrderdate,newDate);
            Ordersetting ordersetting = ordersettingMapper.selectOne(lambdaQueryWrapper);
            if (ordersetting!=null){
                //该日期已经设定的预约信息 更新
                if(number < ordersetting.getReservations()){
                    ordersetting.setNumber(ordersetting.getReservations());
                }else{
                    ordersetting.setNumber(number);
                }
                flag = ordersettingMapper.updateById(ordersetting)>0;
            }else{
                //该日期没有设定预约信息 添加
                ordersetting = new Ordersetting();
                ordersetting.setOrderdate(newDate);
                ordersetting.setNumber(number);
                flag = ordersettingMapper.insert(ordersetting)>0;
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return flag;
    }



}
